package com.mo.lib.utils.time;

import android.os.CountDownTimer;
import android.widget.TextView;

/**
 * @ author：mo
 * @ data：2018/11/7
 * @ 功能：自定义倒计时(CountDownTimer在获取系统时间的、时会有误差，当持续时间长的时候，可能会显示-1)
 */
public abstract class KCountDownTimer extends CountDownTimer {

    /** millisInFuture==从当前时间开始 作用区间 如：5000                countDownInterval ==间隔时间 如：1000 */
    public KCountDownTimer(long millisInFuture, long countDownInterval) {
        super(millisInFuture + 1050, countDownInterval);
    }

    /** 倒计时开始 */
    @Override
    public void onTick(long millisUntilFinished) {
        int second = (int) Math.round((double) millisUntilFinished / 1000) - 1;
        if (second <= 0) {
            onFinish();
        } else {
            onTicks(millisUntilFinished, second);
        }
    }

    /** 倒计时开始 抽象方法       millisUntilFinished ==毫秒数      second ==秒数 */
    protected abstract void onTicks(long millisUntilFinished, int second);

    @Override
    public void onFinish() {

    }

}
